package com.lwj.hiring.talent.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lwj.hiring.common.util.AppUtil;
import com.lwj.hiring.talent.dao.ResumeDao;
import com.lwj.hiring.talent.dao.TalentDao;
import com.lwj.hiring.talent.entity.ResumeEntity;
import com.lwj.hiring.talent.entity.Talent;

/**
 * Resume Servlet
 * 
 * @author Administrator
 *
 */
public class ResumeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private ResumeDao resumeDao = new ResumeDao();
	private TalentDao talentDao = new TalentDao();

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String command = request.getParameter("command");
		if ("list".equals(command)) {
			list(request, response);
		} else if ("delete".equals(command)) {
			delete(request, response);
		} else if ("add".equals(command)) {
			add(request, response);
		} else if ("queryById".equals(command)) {
			queryById(request, response);
		} else if ("update".equals(command)) {
			update(request, response);
		} else if ("companyRead".equals(command)) {
			companyRead(request, response);
		}
	}

	/**
	 * 修改简历
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		String intention = request.getParameter("intention");
		String workExperience = request.getParameter("workExperience");
		String projectExperience = request.getParameter("projectExperience");
		List<Object> list = new ArrayList<>();
		list.add(intention);
		list.add(workExperience);
		list.add(projectExperience);
		list.add(id);
		int row = resumeDao.update(list);
		response.sendRedirect("TalentServlet?command=main&row=" + row);
	}

	/**
	 * 根据id查询简历
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void queryById(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		ResumeEntity resume = resumeDao.queryById(id);
		request.setAttribute("resume", resume);
		request.getRequestDispatcher("view/talent/resume-update.jsp").forward(
				request, response);
	}

	/**
	 * 根据id查询简历
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void companyRead(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		int talentId = Integer.parseInt(request.getParameter("talentId"));
		Talent talent = talentDao.queryById(talentId);
		request.setAttribute("talent", talent);
		ResumeEntity resume = resumeDao.queryById(id);
		request.setAttribute("resume", resume);
		request.getRequestDispatcher("view/talent/resume-info.jsp").forward(
				request, response);
	}

	/**
	 * 新增简历
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void add(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String intention = request.getParameter("intention");
		String workExperience = request.getParameter("workExperience");
		String projectExperience = request.getParameter("projectExperience");
		Talent talent = (Talent) request.getSession().getAttribute("talent");
		int talentId = talent.getId();
		List<Object> list = new ArrayList<>();
		list.add(talentId);
		list.add(intention);
		list.add(workExperience);
		list.add(projectExperience);
		int row = resumeDao.add(list);
		response.sendRedirect("TalentServlet?command=main&row=" + row);
	}

	/**
	 * 删除简历
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		int row = resumeDao.delete(id);

		response.sendRedirect("TalentServlet?command=main&row=" + row);
	}

	/**
	 * 简历列表
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void list(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int id = AppUtil.getTalentId(request, response);
		List<ResumeEntity> resumeList = resumeDao.list(id);
		request.setAttribute("resumeList", resumeList);
		request.getRequestDispatcher("view/talent/main.jsp").forward(request,
				response);

	}

}
